dhcpv4: simplify error handling in dhcpv4_setup_interface()
authorDavid Härdeman <[email protected]>
Fri, 17 Oct 2025 11:57:14 +0000 (13:57 +0200)
committerÁlvaro Fernández Rojas <[email protected]>
Sun, 23 Nov 2025 19:05:03 +0000 (20:05 +0100)
Remove the "ret" variable which can anyway always just be -1 on error,
and rename the "out" label to "error" to clarify that it's only used on
error.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/318
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/config.c
src/dhcpv4.c
src/odhcpd.h

index 865dadfe6b2d025060c0da7d2ee36f070c254dba..01bd375b2e5fc84d69d7c2105cffae2e42873542 100644 (file)
@@ -372,9 +372,7 @@ static void close_interface(struct interface *iface)
        router_setup_interface(iface, false);
        dhcpv6_setup_interface(iface, false);
        ndp_setup_interface(iface, false);
-#ifdef DHCPV4_SUPPORT
        dhcpv4_setup_interface(iface, false);
-#endif
 
        /* make sure timer is not on the timeouts list before freeing */
        uloop_timeout_cancel(&iface->timer_rs);
@@ -1918,17 +1916,13 @@ void reload_services(struct interface *iface)
                router_setup_interface(iface, iface->ra != MODE_DISABLED);
                dhcpv6_setup_interface(iface, iface->dhcpv6 != MODE_DISABLED);
                ndp_setup_interface(iface, iface->ndp != MODE_DISABLED);
-#ifdef DHCPV4_SUPPORT
                dhcpv4_setup_interface(iface, iface->dhcpv4 != MODE_DISABLED);
-#endif
        } else {
                debug("Disabling services with %s not running", iface->ifname);
                router_setup_interface(iface, false);
                dhcpv6_setup_interface(iface, false);
                ndp_setup_interface(iface, false);
-#ifdef DHCPV4_SUPPORT
                dhcpv4_setup_interface(iface, false);
-#endif
        }
 }
 
index c31d521ce3fccd25466caa9599975fbbe7f78700..5d49b76a9a3852a0be68dfd2f397aa73659769bc 100644 (file)
@@ -1435,9 +1435,8 @@ static int dhcpv4_setup_addresses(struct interface *iface)
        return 0;
 }
 
-int dhcpv4_setup_interface(struct interface *iface, bool enable)
+bool dhcpv4_setup_interface(struct interface *iface, bool enable)
 {
-       int ret = 0;
        struct sockaddr_in bind_addr = {
                .sin_family = AF_INET,
                .sin_port = htons(DHCPV4_SERVER_PORT),
@@ -1457,75 +1456,65 @@ int dhcpv4_setup_interface(struct interface *iface, bool enable)
 
                avl_remove_all_elements(&iface->dhcpv4_leases, lease, iface_avl, tmp)
                        dhcpv4_free_lease(lease);
-               return 0;
+               return true;
        }
 
        fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
        if (fd < 0) {
                error("socket(AF_INET): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        /* Basic IPv4 configuration */
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
                error("setsockopt(SO_REUSEADDR): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) < 0) {
                error("setsockopt(SO_BROADCAST): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val)) < 0) {
                error("setsockopt(IP_PKTINFO): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        val = IPTOS_CLASS_CS6;
        if (setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof(val)) < 0) {
                error("setsockopt(IP_TOS): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        val = IP_PMTUDISC_DONT;
        if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)) < 0) {
                error("setsockopt(IP_MTU_DISCOVER): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, iface->ifname,
                       strlen(iface->ifname)) < 0) {
                error("setsockopt(SO_BINDTODEVICE): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
        if (bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
                error("bind(): %m");
-               ret = -1;
-               goto out;
+               goto error;
        }
 
-       if (dhcpv4_setup_addresses(iface) < 0) {
-               ret = -1;
-               goto out;
-       }
+       if (dhcpv4_setup_addresses(iface) < 0)
+               goto error;
 
        iface->dhcpv4_event.uloop.fd = fd;
        iface->dhcpv4_event.handle_dgram = dhcpv4_handle_dgram;
        odhcpd_register(&iface->dhcpv4_event);
-       return 0;
+       return true;
 
-out:
+error:
        close(fd);
-       return ret;
+       return false;
 }
 
 static void dhcpv4_addrlist_change(struct interface *iface)
index 018426f7c1d81cb9073825e10bcca8aa7a53940d..28478abc0cc2ee73110f871b5e310c16415b9dd9 100644 (file)
@@ -644,11 +644,15 @@ int ndp_init(void);
 #ifdef DHCPV4_SUPPORT
 int dhcpv4_init(void);
 void dhcpv4_free_lease(struct dhcpv4_lease *a);
-int dhcpv4_setup_interface(struct interface *iface, bool enable);
+bool dhcpv4_setup_interface(struct interface *iface, bool enable);
 void dhcpv4_handle_msg(void *addr, void *data, size_t len,
                       struct interface *iface, _o_unused void *dest_addr,
                       send_reply_cb_t send_reply, void *opaque);
 #else
+static inline bool dhcpv4_setup_interface(struct interface *iface, bool enable) {
+       return true;
+}
+
 static inline void dhcpv4_free_lease(struct dhcpv4_lease *lease) {
        error("Trying to free IPv4 assignment 0x%p", lease);
 }